﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using TarreOA.TarreOADAL;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class IndexMain : System.Web.UI.Page
{
    public StringBuilder afficheInform;
    public StringBuilder userEmail;
    public StringBuilder userMessage;
    public StringBuilder userDiary;
    public StringBuilder calendarArrange;
    public StringBuilder netDisk;
    public StringBuilder talkHouse;
    public StringBuilder news;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            User curLoginUser = (User) Session["user"];
            IndexMain_Service im_service = new IndexMain_Service();
            Hashtable ht = im_service.FindIndexInfo(curLoginUser);
            userMessage = (StringBuilder)userMessageShow().Append("'");
            userEmail = (StringBuilder) userMail();
            afficheInform = (StringBuilder) ht["AFFICHEINFORM"];
            //userDiary = (StringBuilder) ht["USERDIARY"];
            calendarArrange = (StringBuilder) ht["CURRENTCALENDAR"];
            netDisk = (StringBuilder) ht["NETDISK"];
            //talkHouse = (StringBuilder) ht["TALKHOUSE"];
            news = (StringBuilder) ht["NEWS"];
        }
    }

    /// <summary>
    /// 短消息
    /// </summary>
    /// <returns></returns>
    public StringBuilder userMessageShow()
    {
        SqlConnection sqlConn = null;
        SqlCommand sqlCmd = null;
        SqlDataReader sqlDr = null;
        StringBuilder sbu = new StringBuilder();
        try
        {
            int allCount = 0;
            sqlConn = SQLManager.GetConn();
            sqlConn.Open();
            User curLoginUser = (User) Session["user"];
            string sql = "select count(*) as allcount from sectionempmsgtb2 where inceptUserLoginName = '" + curLoginUser.LoginName + "'";
            sqlCmd = new SqlCommand(sql, sqlConn);
            sqlDr = sqlCmd.ExecuteReader();
            if(sqlDr.Read())
            {
                allCount = (int) sqlDr["allcount"];
            }
            sqlDr.Close();
           sql = "select top 10 SectionEmpMsgtb2.*, usertb.username as 发送人" +
                     " from SectionEmpMsgtb2,usertb where InceptUserLoginName = @uln and usertb.loginname = sectionEmpMsgtb2.SendUserLoginName" +
                     " order by SendDateTime desc";
            sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.Parameters.Add(new SqlParameter("@uln", curLoginUser.LoginName));
            sqlDr = sqlCmd.ExecuteReader();
            if(sqlDr.HasRows)
            {
                while(sqlDr.Read())
                {
                    string content = sqlDr["MsgContent"].ToString();
                    if(content.Length > 20)
                        content = content.Substring(0, 20) + "...";
                    sbu.Append("<li><a href='User/UserNote/MsgContentShow.aspx?mid=" + sqlDr["EmpMsgId"] + "&type=incept'>[" + sqlDr["发送人"].ToString() + "]" + content + "</li></a>");
                }
            }
            sbu.Append("&nbsp;&nbsp;收件箱:<font color='red'>" + allCount + "</font>");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            if(sqlDr != null)
            {
                sqlDr.Close();
                sqlDr.Dispose();
            }
            if(sqlConn.State == ConnectionState.Open)
            {
                sqlConn.Close();
                sqlConn.Dispose();
            }
        }
        return sbu;
    }

    /// <summary>
    /// 文件传阅
    /// </summary>
    /// <returns></returns>
    public StringBuilder userMail()
    {
        SqlConnection sqlConn = null;
        SqlCommand sqlCmd = null;
        SqlDataReader sqlDr = null;
        StringBuilder sbu = new StringBuilder();
        try
        {
            int allCount = 0;
            sqlConn = SQLManager.GetConn();
            sqlConn.Open();
            User curLoginUser = (User) Session["user"];
            string sql = "select count(*) as allcount from EmpEmailMappingtb2 where InceptUserLoginName = @uln";
            sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.Parameters.Add(new SqlParameter("@uln", curLoginUser.LoginName));
            sqlDr = sqlCmd.ExecuteReader();
            if(sqlDr.Read())
            {
                allCount = (int) sqlDr["allcount"];
            }
            sqlDr.Close();
            sql = "select top 10 EmpEmailMappingtb2.*, usertb.username as 发送人 from EmpEmailMappingtb2, usertb "
                + "where EmpEmailMappingtb2.InceptUserLoginName = @uln and "
                + "usertb.loginname = EmpEmailMappingtb2.SendUserLoginName order by SendDateTime desc";
            sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.Parameters.Add(new SqlParameter("@uln", curLoginUser.LoginName));
            sqlDr = sqlCmd.ExecuteReader();
            if(sqlDr.HasRows)
            {
                while(sqlDr.Read())
                {
                    string content = sqlDr["EmailTextContent"].ToString();
                    if(content.Length > 20)
                        content = content.Substring(0, 20) + "...";
                    sbu.Append("<li><a href='User/UserEmail/EmailContent.aspx?eid=" + sqlDr["EmpEmailId"] + "&type=incept'>" + content + "</a></li>");
                }
            }
            sbu.Append("&nbsp;&nbsp;收件箱:<font color='red'>" + allCount + "</font>");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            if(sqlDr != null)
            {
                sqlDr.Close();
                sqlDr.Dispose();
            }
            if(sqlConn.State == ConnectionState.Open)
            {
                sqlConn.Close();
                sqlConn.Dispose();
            }
        }
        return sbu;
    }
}
